import requests
import pprint

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36',
    'referer': 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput='
}


def get_cookies():
    base_url = 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput='
    response = requests.get(base_url, headers=headers)
    return response.cookies.get_dict()


def get_html(cookies):
    url = 'https://www.lagou.com/jobs/positionAjax.json'
    data = {
        'first': 'true',
        'pn': '1',
        'kd': 'python'
    }
    cookies = cookies
    response = requests.post(url, headers=headers, data=data, cookies=cookies)
    return response.json()


def get_data(html):
    for i in range(0, 15):
        city = html['content']['positionResult']['result'][i]['city']
        name = html['content']['positionResult']['result'][i]['companyFullName']
        size = html['content']['positionResult']['result'][i]['companySize']
        education = html['content']['positionResult']['result'][i]['education']
        positionName = html['content']['positionResult']['result'][i]['positionName']
        salary = html['content']['positionResult']['result'][i]['salary']
        workYear = html['content']['positionResult']['result'][i]['workYear']
        result = city + ',' + name + ',' + size + ',' + education + ',' + positionName + ',' + salary + ',' + salary + ',' + workYear
        with open('拉钩职位信息.csv', 'a')as f:
            f.write(result + '\n')


if __name__ == '__main__':
    cookies = get_cookies()
    html = get_html(cookies)
    get_data(html)



